/**
 *
 * Copyright 2009 Bill Garrett (memesis@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License.  You may obtain a copy of the
 * License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License. 
 */
package org.memesis.pandorasbook.util;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * A class which opens a dialog box for exception text.
 *
 * @author garrett
 */
public class ExceptionHandler {
	public static void reportException(Exception ex) {
		JDialog dl = new JDialog();
		dl.setTitle("An exception was thrown");	
		dl.setContentPane(generateExceptionComponent(ex));
		dl.setSize(600, 400);
		dl.setVisible(true);
	}
	
	/**
	 * Given an exception, return a Swing component that displays the exception text.
	 * 
	 * @param ex the exception to represent
	 * @return a Swing component displaying the exception in some fashion
	 */
	public static JComponent generateExceptionComponent(Exception ex) {
		StringWriter writer = new StringWriter();
		ex.printStackTrace(new PrintWriter(writer));
		JTextArea textArea = new JTextArea(writer.toString());
		JScrollPane scrollPane = new JScrollPane(textArea);
		
		return scrollPane;
	}
}
